package cn.xfdzx.android.apps.shop.activity.myshop;


import android.content.Intent;
import android.content.res.ColorStateList;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.luck.picture.lib.tools.ToastUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.adapter.HomeVPAdapter;
import cn.xfdzx.android.apps.shop.base.BaseMvpActivity;
import cn.xfdzx.android.apps.shop.evenbus.SearchGoodsCloseMessage;
import cn.xfdzx.android.apps.shop.evenbus.SearchGoodsContentMessage;
import cn.xfdzx.android.apps.shop.evenbus.UpdateMessage;
import cn.xfdzx.android.apps.shop.fragment.ShopInGoodFragment;
import cn.xfdzx.android.apps.shop.fragment.ShopInGoodLibraryFragment;
import cn.xfdzx.android.apps.shop.util.EmptyUtil;
import cn.xfdzx.android.apps.shop.util.KeybordUtil;
import cn.xfdzx.android.apps.shop.util.ProgressDialog;
import cn.xfdzx.android.apps.shop.util.StatusBarUtil;

import static cn.xfdzx.android.apps.shop.apps.Constants.START_CODE;

/**商品管理页面*/
public class GoodManagementActivity extends BaseMvpActivity {

    @BindView(R.id.iv_back)
    ImageView iv_back;
    @BindView(R.id.ll_state)
    LinearLayout llState;
    @BindView(R.id.tl_fragment_all)
    TabLayout mTabLayout;
    @BindView(R.id.vp_fragment_all)
    ViewPager vpFragmentAll;
    @BindView(R.id.et_goods_search)
    EditText et_goods_search;//搜索商品
    @BindView(R.id.iv_search_delete)
    ImageView iv_search_delete;
    @BindView(R.id.tv_goods_search)
    TextView tv_goods_search;//搜索按钮
    private String[] title;
    private String[] title_1 = { "全部", "正在出售", "未上架"};
    private String[] title_2=  {"商品库","全部", "正在出售", "未上架"};
    private RelativeLayout.LayoutParams item_bar;
    private HomeVPAdapter homeVPAdapter;
    private List<Fragment> mFragments;
    private String type;
    private int currentPosition = 4;

    @Override
    public int getLayoutId() {
        return R.layout.activity_good_management;
    }

    @Override
    public void initView() {
        initHigh();
        EventBus.getDefault().register(this);
        type=getIntent().getStringExtra("type");
        mFragments = new ArrayList<>();
        if (type.equals("0")){
            title=title_2;
            mFragments.add(ShopInGoodLibraryFragment.newInstance("4",type));//
        }else {
            title=title_1;
        }

        mFragments.add(ShopInGoodFragment.newInstance("3",type));//
        mFragments.add(ShopInGoodFragment.newInstance("1",type));//
        mFragments.add(ShopInGoodFragment.newInstance("0",type));//
        mTabLayout.setTabMode(TabLayout.MODE_FIXED);
        mTabLayout.setTabRippleColor(ColorStateList.valueOf(this.getResources().getColor(R.color.picture_color_transparent)));
        for (String tab : title) {
            mTabLayout.addTab(mTabLayout.newTab().setText(tab));
        }
        if (type.equals("0")){
            mTabLayout.setTabTextColors(getResources().getColor(R.color.color_333333), getResources().getColor(R.color.color_FB9129));
            mTabLayout.setSelectedTabIndicatorColor(getResources().getColor(R.color.color_FB9129));
        }else {
            mTabLayout.setTabTextColors(getResources().getColor(R.color.color_333333), getResources().getColor(R.color.color_06B359));
            mTabLayout.setSelectedTabIndicatorColor(getResources().getColor(R.color.color_06B359));
        }

        homeVPAdapter = new HomeVPAdapter(getSupportFragmentManager(), title, mFragments);
        vpFragmentAll.setAdapter(homeVPAdapter);
        vpFragmentAll.setOffscreenPageLimit(2);

        mTabLayout.setupWithViewPager(vpFragmentAll);
        initTextWacherListener();

        vpFragmentAll.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

                if (type.equals("0")){
                    if (position == 1) {
                        currentPosition = 3;
                        EventBus.getDefault().post(new UpdateMessage("3"));
                    } else if (position == 2) {
                        currentPosition = 1;
                        EventBus.getDefault().post(new UpdateMessage("1"));
                    } else if (position == 3) {
                        currentPosition = 0;
                        EventBus.getDefault().post(new UpdateMessage("0"));
                    }else if (position == 0) {
                        currentPosition = 4;
                        EventBus.getDefault().post(new UpdateMessage("4"));
                    }
                }else {
                    if (position == 0) {
                        currentPosition = 3;
                        EventBus.getDefault().post(new UpdateMessage("3"));
                    } else if (position == 1) {
                        currentPosition = 1;
                        EventBus.getDefault().post(new UpdateMessage("1"));
                    } else if (position == 2) {
                        currentPosition = 0;
                        EventBus.getDefault().post(new UpdateMessage("0"));
                    }
                }

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        initOnPreDrawListener();

        if (type.equals("0")){
            vpFragmentAll.setCurrentItem(getIntent().getIntExtra("position",0));
        }else {
            if (getIntent().getIntExtra("position",0)>0){
                vpFragmentAll.setCurrentItem(getIntent().getIntExtra("position",0)-1);
            }

        }
    }

    private void initTextWacherListener() {
        et_goods_search.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (editable .toString().isEmpty()) {
                    iv_search_delete.setVisibility(View.INVISIBLE);

                    EventBus.getDefault().post(new SearchGoodsContentMessage(et_goods_search.getText().toString().trim(),0,currentPosition));
                } else {
                    iv_search_delete.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    private void initOnPreDrawListener() {
        final ViewTreeObserver viewTreeObserver = this.getWindow().getDecorView().getViewTreeObserver();
        viewTreeObserver.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                if (type.equals("0")){
                    if (vpFragmentAll.getCurrentItem() == 1) {
                        EventBus.getDefault().post(new UpdateMessage("3"));
                    } else if (vpFragmentAll.getCurrentItem() == 2) {
                        EventBus.getDefault().post(new UpdateMessage("1"));
                    } else if (vpFragmentAll.getCurrentItem() == 3) {
                        EventBus.getDefault().post(new UpdateMessage("0"));
                    }
                }else {
                    if (vpFragmentAll.getCurrentItem() == 0) {
                        EventBus.getDefault().post(new UpdateMessage("3"));
                    } else if (vpFragmentAll.getCurrentItem() == 1) {
                        EventBus.getDefault().post(new UpdateMessage("1"));
                    } else if (vpFragmentAll.getCurrentItem() == 2) {
                        EventBus.getDefault().post(new UpdateMessage("0"));
                    }
                }

                // 移除OnPreDrawListener事件监听
                getWindow().getDecorView().getViewTreeObserver().removeOnPreDrawListener(this);

                return true;
            }
        });
    }
    private void initHigh() {
        item_bar = (RelativeLayout.LayoutParams) llState.getLayoutParams();
        item_bar.height = StatusBarUtil.getStatusBarHeight(this);
        llState.setLayoutParams(item_bar);
    }

    @Override
    public void showLoading() {
        if (!ProgressDialog.getInstance().materialDialog.isShowing()) {
            ProgressDialog.getInstance().show(this);
        }
    }

    @Override
    public void hideLoading() {
        ProgressDialog.getInstance().dismiss();
    }

    @Override
    public void onError(Throwable throwable) {

    }

    @Override
    protected boolean isUseFullScreenMode() {
        return true;
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

        //刷新所有
        if (type.equals("0")){
            if (vpFragmentAll.getCurrentItem() == 1) {
                EventBus.getDefault().post(new UpdateMessage("3"));
            } else if (vpFragmentAll.getCurrentItem() == 2) {
                EventBus.getDefault().post(new UpdateMessage("1"));
            } else if (vpFragmentAll.getCurrentItem() == 3) {
                EventBus.getDefault().post(new UpdateMessage("0"));
            }
        }else {
            if (vpFragmentAll.getCurrentItem() == 0) {
                EventBus.getDefault().post(new UpdateMessage("3"));
            } else if (vpFragmentAll.getCurrentItem() == 1) {
                EventBus.getDefault().post(new UpdateMessage("1"));
            } else if (vpFragmentAll.getCurrentItem() == 2) {
                EventBus.getDefault().post(new UpdateMessage("0"));
            }
        }

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == START_CODE) {
                //刷新所有
                if (type.equals("0")){
                    if (vpFragmentAll.getCurrentItem() == 1) {
                        EventBus.getDefault().post(new UpdateMessage("3"));
                    } else if (vpFragmentAll.getCurrentItem() == 2) {
                        EventBus.getDefault().post(new UpdateMessage("1"));
                    } else if (vpFragmentAll.getCurrentItem() == 3) {
                        EventBus.getDefault().post(new UpdateMessage("0"));
                    }else if (vpFragmentAll.getCurrentItem() == 0){
                        currentPosition = 4;
                    }
                }else {
                    if (vpFragmentAll.getCurrentItem() == 0) {
                        EventBus.getDefault().post(new UpdateMessage("3"));
                    } else if (vpFragmentAll.getCurrentItem() == 1) {
                        EventBus.getDefault().post(new UpdateMessage("1"));
                    } else if (vpFragmentAll.getCurrentItem() == 2) {
                        EventBus.getDefault().post(new UpdateMessage("0"));
                    }
                }



            }
        }
    }


    @OnClick({R.id.iv_back,R.id.tv_goods_search,R.id.iv_search_delete})
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_goods_search:
                if (EmptyUtil.isNotEmpty(et_goods_search.getText().toString().trim())){
                    EventBus.getDefault().post(new SearchGoodsContentMessage(et_goods_search.getText().toString().trim(),1,currentPosition));
                }else {
                    ToastUtils.s(this,"请输入商品名称");
                    return;
                }

                break;
            case R.id.iv_search_delete:
                et_goods_search.getText().clear();
                EventBus.getDefault().post(new SearchGoodsContentMessage(et_goods_search.getText().toString().trim(),0,currentPosition));
                break;

        }
    }

    @Subscribe(threadMode = ThreadMode.POSTING)
    public void onSearchEvent(SearchGoodsCloseMessage messageEvent) {
       //商品库页面选了小分类清除搜索框搜索

        et_goods_search.getText().clear();

        KeybordUtil.hideKeyboard(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }
}
